package com.wd.parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wd.util.StringUtil;

/**
 * 
 * @author Nassir Wen
 *
 */
public class ParserHtml {
	
	public static String getTrafficRankFromPage(String page){
		String reg = "<img src=\"/images/icons/globe-sm.jpg\".*?/>[\\s|,|\\d]*</div>";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = "[\\s]*[,|\\d]+[\\s]+";
		content = isMatchContent(content, reg);
		content = (content == null)?content:content.trim();
		return content;
	}
	
	public static String getTrafficRankCnFromPage(String page){
		String reg = "<div class=\"data\"><img.*?/>[\\s|,|\\d]*</div>";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = "[\\s]*[,|\\d]+[\\s]+";
		content = isMatchContent(content, reg);
		content = (content == null)?content:content.trim();
		return content;
	}
	
	public static String getSiteLinkingInFromPage(String page){
		String reg = "<div class=\"data\"><a class=\"data-color2 \".*?>[\\s|,|\\d]*</a></div>";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = ">[\\s]*[,|\\d]+[\\s]+";
		content = isMatchContent(content, reg);
		if(content == null){
			return content;
		}
		reg = "[,|\\d]+";
		content = isMatchContent(content, reg);
		content = (content == null)?content:content.trim();
		return content;
	}
	
	public static String getPageviewsGlobalPercentFromPage(String page){
		String reg = "<div id=\"pageviews\".*?7 day";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = "Yesterday</th><td class=.*?>[\\d]+[\\.]?[\\d]*%";
		content = isMatchContent(content, reg);
		if(content == null){
			reg = "访问量占全球页面访问量的百分比.*?最近一月平均";
			content = isMatchContent(page, reg);
			if(content == null){
				return content;
			}
		}
		reg = "[\\d]+[\\.]?[\\d]*";
		content = isMatchContent(content, reg);
		content = (content == null)?content:content.trim();
		return content;
	}
	
	public static String getPageviewsPerUserFromPage(String page){
		String reg = "<div id=\"pageviews_per_user\".*?7 day";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = "Yesterday</th><td class=.*?>[\\d]+[\\.]?[\\d]*";
		content = isMatchContent(content, reg);
		if(content == null){
			reg = "每日人均页面访问量.*?最近一月平均";
			content = isMatchContent(page, reg);
			if(content == null){
				return content;
			}
		}
		reg = "[\\d]+[\\.]?[\\d]*";
		content = isMatchContent(content, reg);
		content = (content == null)?content:content.trim();
		return content;
	}
	
	public static String getseRefererPercentagePage(String page){
		String reg = "<div id=\"search\".*?7 day";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = "Yesterday</th><td class=.*?>[\\d]+[\\.]?[\\d]*%";
		content = isMatchContent(content, reg);
		if(content == null){
			reg = "来源于搜索引擎的百分比.*?最近一月平均";
			content = isMatchContent(page, reg);
			if(content == null){
				return content;
			}
		}
		reg = "[\\d]+[\\.]?[\\d]*";
		content = isMatchContent(content, reg);
		content = (content == null)?content:content.trim();
		return content;
	}
	
	public static String get3rdMGrowthMovement(String page){
		String reg = "<div id=\"rank\".*?<th>3 month</th>.*?class=\"percent \">.*?</td>";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return null;
		}
		reg = "<th>3 month</th>.*?class=\"percent \">.*?</td>";
		content = isMatchContent(content, reg);
		if(content == null){
		    return content;
		}
		reg = "<td class=\"percent \">-?\\+?[,|\\d]+\\s*</td>";
		content = isMatchContent(content, reg);
		if(content == null){
		    return content;
		}
		reg = "-?\\+?[,|\\d]+\\s*";
		content = isMatchContent(content, reg);
		if(content == null){
		    return content;
		}
		
		content = content.replace(",", "").replace("+", "");
		content = (content == null)?content:content.trim();
		return content;
	}
	
	public static String getSubDomainSpreadFromPage(String page,String domain){
		String reg = "Subdomain.*<span id=\"moreTraffic\"";
		String content = null;
		String percent = null;
		String domainName = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = "[\\d]+[\\.]+[\\d]*%";
		percent = isMatchContentAll(content, reg);
		reg = "[\\w.]*(\\.com|\\.cn|\\.net)";
		domainName = isMatchContentAll(content, reg);
		return percent+"#"+domainName;
	}
	
	public static String getAvgLoadTimeFromPage(String page){
		String reg = "Average Load Time for.*Seconds";
		String content = null;
		if(!StringUtil.isValidString(page)){
			return content;
		}
		content = isMatchContent(page, reg);
		if(content == null){
			return content;
		}
		reg = "[\\d]+[\\.]?[\\d]+";
		content = isMatchContent(content, reg);
		content = (content == null)?content:content.trim();
		return content;
	}
	
	private static String isMatchContent(String s,String regex){
		String content = null;
		//ignore Case
		Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(s);
		while(m.find()) {
			content = m.group();
			break;
		}
		return content;
	}
	
	private static String isMatchContentAll(String s,String regex){
		StringBuffer content = new StringBuffer();
		//ignore Case
		Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(s);
		while(m.find()) {
			if(content.length() > 0){
				content.append("|");
			}
			content.append(m.group());
		}
		return content.toString();
	}
}
